{% extends "page.html" %}

{% block title %}Sign In{% endblock %}

{% block extra-head %}
<script type="text/javascript">
var signIn = namespace.lookup("com.pageforest.auth.sign-in");
$(document).ready(function() {
    var username = "{{ user.username }}";
    var appId = "{{ cross_app.get_app_id }}";
    signIn.onReady(username, appId);
});
</script>
{% endblock %}

{% block both-columns %}

<h2>
{% if user %}
    {{ user.username }}, you are signed in to {{ SITE_NAME }}
    <span style="display:none;" class="have_app">and {{ cross_app.title }} ({{ cross_app.get_app_id }})</span>
{% else %}
    Sign in to {{ SITE_NAME }}
    {% if cross_app %} and {{ cross_app.title }} ({{ cross_app.get_app_id }}){% endif %}
{% endif %}
</h2>

{{ enablejs|safe }}
{{ httponly|safe }}

{% if cross_app %}
    <p class="want_app">The application, <b>{{ cross_app.title }} ({{ cross_app.get_app_id }})</b>,
      is requesting permission to save documents on {{ SITE_NAME }}.
    </p>
{% endif %}

{% if form.non_field_errors %}
    <p class="error">{{ form.non_field_errors.0 }}</p>
{% endif %}

<form id="sign-in" method="post" onsubmit="return signIn.onSubmit()">
    {{ csrf_token }}
    <table>
    {% if user %}
        {% if cross_app %}
            <input class="want_app" type="button" value="Allow Access" onclick="return signIn.transferSession('{{ session_key }}')" />
        {% endif %}
    {% else %}
        {% for field in form %}
            <tr>
              <td>{{ field.label_tag }}:</td>
              <td>{{ field }}</td>
              <td class="form-message" id="validate_{{ field.html_name }}">
                 <span class="error">{{ field.errors.0 }}</span>
              </td>
            </tr>
        {% endfor %}
        <tr><td></td>
          <td><input class="want_app" type="submit" value="Sign In" disabled="disabled" /></td>
          <td></td>
        </tr>
    {% endif %}
    </table>
</form>

{% if user %}
    {% if not user.email_verified %}
        <p class="error">Check your email for an <a href="/email-verify">email verification</a> message.</p>
    {% endif %}
    <p><a href="/sign-out/" onclick="signIn.signOut();return false;">Sign Out</a></p>
{% else %}
    <p>Need to create account?  <a href="/sign-up">Join Now</a>.</p>
{% endif %}

{% endblock both-columns %}
